﻿using System.Web.Mvc;
using Business;
using Dto;
using Manage.Models;

namespace Manage.Controllers
{
    public class ManageMenuController : Controller
    {
        private readonly ManageMenuBusiness _manageMenuBusiness = new ManageMenuBusiness();

        public ActionResult EditMenu(int? id)
        {
            var adminModel = Session["Admin"] as AdminDto ?? new AdminDto();
            var dto = _manageMenuBusiness.GetMenuById(id ?? 0);
            var menuList = _manageMenuBusiness.GetMenuBySuperiorId
                (0, false, adminModel.RoleDto.RoleContent.Split(new[] { ',' }));
            foreach (var menu in menuList)
            {
                menu.ChildMenuList = _manageMenuBusiness.GetMenuBySuperiorId
                    (menu.MenuID, true, adminModel.RoleDto.RoleContent.Split(new[] { ',' }));
            }
            ViewData["MenuList"] = menuList;
            return View(dto ?? new ManageMenuDto());
        }

        public ActionResult SaveMenu(ManageMenuModel model)
        {
            var dto = new ManageMenuDto
                          {
                              MenuID = model.MenuID,
                              MenuKey = model.MenuKey,
                              MenuName = model.MenuName,
                              MenuOldKey = model.MenuOldKey,
                              MenuOrder = model.MenuOrder,
                              MenuPath = model.MenuPath,
                              MenuSuperiorID = model.MenuSuperiorId,
                          };
            var b = _manageMenuBusiness.SaveMenu(dto);
            if (b)
            {
                Response.Write("<script>alert('保存成功');location.href='" + Url.Action("EditMenu") + "';</script>");
                Response.End();
            }
            return RedirectToAction("EditMenu");
        }

        public ActionResult DeleteMenu(int id)
        {
            var b = _manageMenuBusiness.DeleteMenu(id);
            if (b)
            {
                Response.Write("<script>alert('删除成功');location.href='" + Url.Action("EditMenu") + "';</script>");
                Response.End();
            }
            return RedirectToAction("EditMenu");
        }
    }
}